package Sgdb::TopMenu ;
use Sgdb::Utils qw( :DEFAULT
                  ) ;

# pragmas for all
use warnings ;
use strict ;
use integer ;
use bytes ;

# @topmenu items are:
# 1. ref '' (string) : considered as the (maybe) new current submenu
# 2. ref ARRAY : will be added to current submenu
# 3. ref CODE  : will be executed, with current submenu parameter
# 4. '-' : separator

# PanesWin
my $step_disasm      = pref "disassemble-while-stepping" ;
# my $moduleoffset     = pref "disassemble-module-offset"  ;
my $highlightpathces = pref "highlight-patches-in-source" ;

my @topmenu = (
               'File',
               ['File ..', \&Sgdb::ServicePane::load_elf_file],
               \&Sgdb::ServicePane::top_menu_recent_files,
               # \&Sgdb::ServicePane::top_menu_recent_targets,
               '-',
               # ['command',
               #  -label     => 'Test Bench ..',
               #  -command   => \&Sgdb::ServicePane::test_bench,
               # ],
               # '-',
               [ 'Exit', sub { warn "all done\n" ;
                               $mainwindow -> destroy() ;
                               Tk::exit( 0 )
                             }, -underline => 1 ],
               ( $ENV{'SGDBTK_DEBUG'} ? [ 'stoppoint ..', \&::stoppoint ] : () ) ,

               'Source',
               [ 'Clear All Disassembly', [\&hook_run, 'clear-all-disassembly' ], -underline => 1 ],
               [ 'Close All Sources'    , [\&hook_run, 'close-all-sources'     ],   ],
               [['checkbutton',
                 -label     => 'Disassemble Frames',
                 -variable  => \$step_disasm,
                 -command   => sub {
                     pref_set( 'disassemble-while-stepping', $step_disasm)
                 } ]],
               # [['checkbutton',
               #   -label     => 'Always Highlight Pathces',
               #   -variable  => \$highlightpathces,
               #   -command   => sub {
               #       pref_set( 'highlight-patches-in-source', $highlightpathces)
               #   } ]],
               '-' ,
               # [ 'Dump Patches',  [\&hook_run_lock, 'dump-all-patches'    ] ],
               [ 'Close Current', [\&hook_run, 'close-current-source-pane'] ],

               'View',
               [ 'Messages..', [\&hook_run, 'show-data-messages'    ] ],
               '-' ,
               [ 'Service Pane ..', [\&hook_run, 'add-service-pane' ] ],
               # '-' ,
               # [ 'Swuch Window ..', [\&hook_run, 'add-swuch-window' ] ],
               # [ 'Swuch Pane   ..', [\&hook_run, 'add-swuch-pane'   ], -underline => 14],
               # [ 'Inlines Pane (old) ..', [\&hook_run, 'add-inlines-right-pane'] ],
               # '-',
               # [ 'Profiler ..', [\&hook_run, 'add-profiler-pane'    ] ],
               # [ 'Profiler-Passive ..', [\&hook_run, 'add-passive-prof-pane'], -underline => 1 ],
               # [ 'Profiler-Branch ..', [\&hook_run, 'add-branch-prof-pane'], -underline => 9 ],
               # '-' ,
               # [ 'Run Symmevent', [\&hook_run, 'run-event-trace-app' ] ],
               # ( (pref 'menu-show-old-event-filter') ?
               # [ 'Event Filter ..', [\&hook_run, 'add-ev-filter-pane'] ] :
               #   () ),
               '-' ,
               [ 'Customize ..', [\&hook_run, 'add-preferences-pane' ] ],

               # 'Traces',
               # [ 'Tracepoints..', [\&hook_run_lock, 'show-data-tracepoints'] ],
               # [ 'Actions..', [\&hook_run_lock, 'set-tracepoint-action', 1 ] ],
               # [ 'Status..', [\&hook_run_lock, 'add-status-pane'           ] ],
               # # [ 'Monitor..', [\&hook_run_lock, 'add-monitor-pane'         ] ],
               # '-' ,
               # [ 'Fix Checker (pane)', [\&hook_run_lock, 'show-emc-checkfix'  ] ],
               # [ 'Fix Checker (window)', [\&hook_run_lock, 'show-other-emc-checkfix' ], -underline => 13 ],
               # [ 'Fix Create (pane)', [\&hook_run_lock, 'show-emc-make-patch' ] ],
               # '-' ,
               # [ 'delete all', [\&gdb_exec_lock, 'delete tracepoints'] ],
               # [ 'traces status', [\&gdb_exec_lock, 'tstatus'] ],
               # '-' ,
               # \&Sgdb::DataTracepoints::top_menu_recover_tracepoints,

               'Display',
               [ 'Registers..', [\&hook_run, 'add-display-registers'] ],
               [ 'Watch..', [\&hook_run, 'add-display-watch'] ],
               [ 'Locals..', sub {
                    busy ;
                    hook_run 'add-watch', 'info locals' ;
                    hook_run 'add-watch', 'info args' ;
                    unbusy ;
                } ],
               # [ 'Dump Collect', [\&hook_run, 'add-display-collected'] ],
               '-' ,
               [ 'Symbol Lookup', [\&hook_run, 'symbol-lookup'] ],
               [ 'Usage  Lookup', [\&hook_run, 'usage-lookup' ] ],
               [ 'Etags  Lookup', [\&hook_run, 'etags-lookup' ] ],
               '-' ,
               [ 'Breakpoints..', [\&hook_run_lock, 'show-data-breakpoints'   ] ],
               [ 'Tracepoints..', [\&hook_run_lock, 'show-data-tracepoints'   ] ],
               # [ 'Function call..', [\&hook_run_lock, 'add-function-call-pane'] ],
               '-',
               [ 'Close Current Pane', [\&hook_run, 'close-current-data-pane' ] ],

               'Help',
               \&Sgdb::StatusLine::top_menu_show_balloon_help,
               '-' ,
               # [ 'FAQ Help ..',   [\&hook_run, 'show-helper-window'          ] ],
               [ 'Short Keys ..', [\&hook_run, 'display-shortcut-keys', 'key'] ],
              ) ;
init () ;

sub init { 1 }

sub new {

    my $self = shift or die "whoami";
    my $topf = shift or die "top frame needed" ;

    my $menu = $topf -> menu ;
    $menu -> delete (0, 'end') ;           # cleanup (who asked them to initialize menus anyway?)
    $menu -> configure ( pref 'top-menu' ) ;

    menu_fill $menu, 'top-menu', @topmenu ;

    @topmenu = () ;             # Clear some memory

    hook_add('reconfigure-all' => [\&reconfigure_topmenu, $menu]) ;

    $menu ;
}

sub reconfigure_topmenu {

    my @menus = @_ ;

    while (my $topmenu = shift @menus) {   # tailed recursive loop

        $topmenu -> configure( pref( 'top-menu') ) ;

        my $end = $topmenu -> index( 'end' ) ;
        $end =~ /\d/ or next ;
        foreach my $entry ( 0 .. $end ) {

            push @menus, $topmenu->entrycget($entry, '-menu') if
                $topmenu->type($entry) eq 'cascade' ;
        }
    }
}


